package com.xiaoxiong.gobang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xiaoxiong.gobang.R;
import com.xiaoxiong.gobang.ai.FirstAi;
import com.xiaoxiong.gobang.bean.ComputerPlayer;
import com.xiaoxiong.gobang.bean.HumanPlayer;
import com.xiaoxiong.gobang.bean.Player;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.ui
 * @Description:首页
 * @email 249900679@qq.com
 * @date 2016/4/3 0:49
 */
public class IndexActivity extends BaseActivity {

    private EditText etName1;
    private EditText etName2;
    private EditText etDepth1;
    private EditText etDepth2;

    private RadioGroup rg1;
    private RadioGroup rg2;

    private Player player1;
    private Player player2;
    private int p1, p2;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.index_layout);

        rg1 = (RadioGroup) findViewById(R.id.rg1);
        rg2 = (RadioGroup) findViewById(R.id.rg2);

        etName1 = (EditText) findViewById(R.id.etn1);
        etName2 = (EditText) findViewById(R.id.etn2);
        etDepth1 = (EditText) findViewById(R.id.et1);
        etDepth2 = (EditText) findViewById(R.id.et2);
        etDepth2.setVisibility(View.GONE);

        p2 = 1;
        p1 = 2;
        player1 = player2 = null;

        button = (Button) findViewById(R.id.submit);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n1 = etName1.getText().toString();
                String n2 = etName2.getText().toString();
                String d1 = "";
                String d2 = "";
                if (n1.equals("") || n2.equals("")) {
                    player1 = player2 = null;
                    Toast.makeText(getApplicationContext(), "请输入玩家名称！", Toast.LENGTH_SHORT).show();
                } else {
                    if (p1 == 2) {
                        d1 = etDepth1.getText().toString();
                        if (d1.equals("")) {
                            player1 = null;
                            Toast.makeText(getApplicationContext(), "请输入AI搜索深度！", Toast.LENGTH_SHORT).show();
                        } else {
                            int intd1 = Integer.parseInt(d1);
                            if (intd1 > 0 && intd1 < 3) {
                                player1 = new ComputerPlayer(n1);
                                FirstAi ai = new FirstAi();
                                ((ComputerPlayer) player1).SetSearcher(ai, Integer.parseInt(d1));
                            } else {
                                player1 = null;
                                Toast.makeText(getApplicationContext(), "AI搜索深度为1~2！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        player1 = new HumanPlayer(n1);
                    }
                    if (p2 == 2) {
                        d2 = etDepth2.getText().toString();
                        if (d2.equals("")) {
                            player2 = null;
                            Toast.makeText(getApplicationContext(), "请输入AI搜索深度！", Toast.LENGTH_SHORT).show();
                        } else {
                            int intd2 = Integer.parseInt(d2);
                            if (intd2 > 0 && intd2 < 3) {
                                player2 = new ComputerPlayer(n2);
                                FirstAi ai = new FirstAi();
                                ((ComputerPlayer) player2).SetSearcher(ai, Integer.parseInt(d2));
                            } else {
                                player2 = null;
                                Toast.makeText(getApplicationContext(), "AI搜索深度为1~2！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        player2 = new HumanPlayer(n2);
                    }
                }
                if (player1 != null && player2 != null) {
                    Intent in = new Intent(IndexActivity.this, GameActivity.class);
                    in.putExtra("playername1", n1);
                    in.putExtra("playername2", n2);
                    if (p1 == 2) {
                        in.putExtra("playerdepth1", Integer.parseInt(d1));
                    }
                    if (p2 == 2) {
                        in.putExtra("playerdepth2", Integer.parseInt(d2));
                    }
                    IndexActivity.this.startActivity(in);
                    Log.d("player", player1.GetPlayerName());
                    Log.d("player", player2.GetPlayerName());
                }
            }
        });

        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb11:
                        etDepth1.setVisibility(View.GONE);
                        p1 = 1;
                        Log.d("id", "人");
                        break;
                    case R.id.rb12:
                        etDepth1.setVisibility(View.VISIBLE);
                        p1 = 2;
                        Log.d("id", "AI");
                        break;
                }
            }
        });
        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb21:
                        etDepth2.setVisibility(View.GONE);
                        p2 = 1;
                        Log.d("id", "人");
                        break;
                    case R.id.rb22:
                        etDepth2.setVisibility(View.VISIBLE);
                        p2 = 2;
                        Log.d("id", "AI");
                        break;
                }
            }
        });
    }
}
